from rest_framework.generics import RetrieveAPIView, GenericAPIView
from recruit.serializers.detail_serializer import *
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import action


class RecuitCollectionViewSet(ModelViewSet):
    queryset = Recruit.objects.all()

    # 收藏职位 recruit/{pk}/collect/
    @action(methods=['post'],detail=True,url_path='collect')
    def collect(self,request,pk):
        try:
            user = request.user
        except Exception:
            user = None

        if user is not None and user.is_authenticated:
            recruit = self.get_object()
            recruit.users.add(user)
            recruit.save()
            return Response({'success':True,'message':'收藏成功'})
        else:
            return Response({'success':False,'message':'未登录'})

class RecruitDetailView(RetrieveAPIView):
    """职位详情页 接口"""

    queryset = Recruit.objects.all()
    serializer_class = RecruitModelSerializer
    lookup_field = 'id'


# class ReCollectionView(GenericAPIView):
#     """收藏职位接口"""
#     queryset = Recruit.objects.all()
#     lookup_field = 'id'
#
#     def post(self, request, id):
#
#         # token = request.query_params.get('token')
#         # token = request.META['token']
#         # content_type = request.META['Content-Type']
#         # if not token:
#         #     return Response({'message': '未登录，请登录后再重试！', 'success': False})
#
#         # user = jwt_response_payload_handler(token)
#         user = request.user
#
#         if not user:
#             return Response({'message': '未登录，请登录后再重试！', 'success': False})
#         # try:
#         #     # user = request.auth
#         #     user = request.user
#         # except Exception as e:
#         #     user = None
#
#         # if user is not None and user.is_authenticated:
#
#         if user is not None and user.is_authenticated:
#             recruit = self.get_object()
#             recruit.users.add(user)
#             recruit.save()
#             return Response({'message': '收藏成功！', 'success': True})
#
#         else:
#             return Response({'message': '未登录，请登录后再重试！', 'success': False})


# class ReCollectionView2(APIView):
#     "收藏职位 方案二"
#     def post(self,request,id):
#         user = request.user
#         enterprise = Enterprise.objects.get(id=id)
#         user = User.objects.get(username=user.username)
#         enterprise.users.add(user)
#         enterprise.save()
#         return Response({'message': '收藏成功！', 'success': True})


class ReCancelCollectionView(GenericAPIView):
    """取消收藏职位接口"""
    queryset = Recruit.objects.all()
    lookup_field = 'id'

    def post(self, request, id):
        try:
            user = request.user
        except Exception as e:
            user = None

        if user is not None and user.is_authenticated:
            recruit = self.get_object()
            recruit.users.remove(user)
            recruit.save()
            return Response({'message': '取消收藏成功！', 'success': True})
        else:
            return Response({'message': '未登录，请登录后再重试！', 'success': False})


class EnterpriseDetailView(RetrieveAPIView):
    queryset = Enterprise.objects.all()
    serializer_class = EnterpriseModelSerializer
    lookup_field = 'id'


class EnterCollectionView(GenericAPIView):
    """收藏企业接口"""
    queryset = Enterprise.objects.all()
    lookup_field = 'id'

    def post(self, request, id):
        try:
            user = request.user
        except Exception as e:
            user = None

        if user is not None and user.is_authenticated:
            enterprise = self.get_object()
            enterprise.users.add(user)
            enterprise.save()
            return Response({'message': '收藏成功！', 'success': True})
        else:
            return Response({'message': '未登录，请登录后再重试！', 'success': False})


class EnterCancelCollectionView(GenericAPIView):
    """收藏企业接口"""
    queryset = Enterprise.objects.all()
    lookup_field = 'id'

    def post(self, request, id):
        try:
            user = request.user
        except Exception as e:
            user = None

        if user is not None and user.is_authenticated:
            enterprise = self.get_object()
            enterprise.users.remove(user)
            enterprise.save()
            return Response({'message': '取消收藏成功！', 'success': True})
        else:
            return Response({'message': '未登录，请登录后再重试！', 'success': False})
